﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using VPIC_EDILib;
using System.IO;
using System.Xml.Linq;
using System.Reflection;
namespace VPIC_EDIConsole
{
    class Program
    {
        private static void Writelog(string message)
        {
            FileInfo fileLog = new FileInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\log.txt");

            if (fileLog.Exists)
            {
                StreamWriter writer = fileLog.AppendText();
                writer.WriteLine(message);
                writer.Flush();
                writer.Close();
            }
            else
            {
                StreamWriter writer = fileLog.CreateText();
                writer.WriteLine(message);
                writer.Flush();
                writer.Close();

            }


        }
        static void Main(string[] args)
        {
            try
            {
                //args 0 : Export file 830 forecast , 1:862 Shipping Schedules
                Config itemConfig = new Config();

                itemConfig = itemConfig.LoadConfig();
                Writelog(itemConfig.ServerName);
                DirectoryInfo dirInfo = new DirectoryInfo(itemConfig.ShareFolderCSV);
                foreach (FileInfo fileEDI in dirInfo.GetFiles())
                {

                    Writelog(fileEDI.FullName);
                    while (fileEDI.IsReadOnly)
                    {

                    }
                    string type = "";
                    StreamReader ediReader = fileEDI.OpenText();
                    int i = 0;
                    while (ediReader.Peek() > 0)
                    {
                        if (i > 0)
                        {

                            string line = ediReader.ReadLine();
                            string[] tokens = line.Split(new char[1] { ',' });
                            type = tokens[0];
                        }
                        i++;

                    }
                    ediReader.Close();
                    XMLProcess XMLProcess = new XMLProcess(itemConfig);


                    if (type == "830")
                    {
                        List<EDIEntity> ListEDI = new List<EDIEntity>();
                        ListEDI = XMLProcess.GetListEDIEntitySOForecast(fileEDI);
                        string filename = XMLProcess.GetXMLSOForecast(ListEDI);
                        XMLProcess.StartProcessImportInvoice(filename);
                       

                        fileEDI.MoveTo(dirInfo.FullName + "\\Imported\\" + fileEDI.Name + DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss"));
                    }
                    if (type == "862")
                    {
                        List<EDIEntityShip> ListEDI = new List<EDIEntityShip>();
                        ListEDI = XMLProcess.GetListEDIEntitySOShip(fileEDI);
                        string filename = XMLProcess.GetXMLSOShippingSchedules(ListEDI);
                        XMLProcess.StartProcessImportInvoice(filename);
                     
                        fileEDI.MoveTo(dirInfo.FullName + "\\Imported\\" + fileEDI.Name + DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss"));
                    }

                }
            }
            catch (Exception ex)
            {
                Writelog(ex.Message);
            }
        }
    }
}
